"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】创建事件触发器函数指定函数字段验证
Create At   :   2024/07/13
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0038()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
    测试步骤：
        1、create EVENT TRIGGER evtri1_eventtrigger_0038 ON ddl_command_start execute procedure f_eventtrigger_0038();
        2、create EVENT TRIGGER evtri2_eventtrigger_0038 ON ddl_command_start execute FUNCTION f_eventtrigger_0038();
        3、create EVENT TRIGGER evtri3_eventtrigger_0038 ON ddl_command_start execute XXX f_eventtrigger_0038();
Expect      : 
        1、创建成功
        2、创建成功
        3、创建失败
History     :
        modified by nanyang12 2025/3/18: 研发代码变更，增加支持execute FUNCTION
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri1_eventtrigger_0038;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri2_eventtrigger_0038;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri3_eventtrigger_0038;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0038();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0038() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "指定procedure创建事件触发器:create EVENT TRIGGER evtri1_eventtrigger_0038 ON ddl_command_start execute procedure f_eventtrigger_0038();")
        msg = self.comsh.execut_db_sql(
            "create EVENT TRIGGER evtri1_eventtrigger_0038 ON ddl_command_start execute procedure f_eventtrigger_0038();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

        self.logger.info(
            "指定FUNCTION创建事件触发器:create EVENT TRIGGER evtri2_eventtrigger_0038 ON ddl_command_start execute FUNCTION f_eventtrigger_0038();")
        msg = self.comsh.execut_db_sql(
            "create EVENT TRIGGER evtri2_eventtrigger_0038 ON ddl_command_start execute FUNCTION f_eventtrigger_0038();")
        self.logger.info(msg)
        self.assertIn('CREATE EVENT TRIGGER', msg)

        self.logger.info(
            "指定XXX创建事件触发器:create EVENT TRIGGER evtri3_eventtrigger_0038 ON ddl_command_start execute XXX f_eventtrigger_0038();")
        msg = self.comsh.execut_db_sql(
            "create EVENT TRIGGER evtri3_eventtrigger_0038 ON ddl_command_start execute XXX f_eventtrigger_0038();")
        self.logger.info(msg)
        self.assertIn('ERROR:  syntax error at or near "XXX"', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri1_eventtrigger_0038;")
        self.logger.info(msg)
        self.assertIn('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri2_eventtrigger_0038;")
        self.logger.info(msg)
        self.assertIn('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0038();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
